package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

import java.util.Map;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
@Api("注册验证")
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private RedisCache redisCache;

    @PostMapping("/register")
    @ApiOperation("注册验证")
    public AjaxResult register(@ApiParam(name="注册验证参数",value="传入json格式",required=true)@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        //验证短信信息
        String verifyKey = CacheConstants.SMS_CAPTCHA_CODE_KEY + user.getSmsid();
        Map<String, Object> smsCode =  redisCache.getCacheObject(verifyKey);
        if(StringUtils.isEmpty(user.getSms())){
            return error("验证码不能为空");
        }
        if(smsCode == null) {
            return error("验证码失效");
        }
        String applyMobile = (String) smsCode.get("mobile");
        int code = (int) smsCode.get("code");
        if(!applyMobile.equals(user.getMobile())) {
            return error("手机号码不一致");
        }
        if(code != Integer.parseInt(user.getSms())) {
            return error("验证码错误");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    @GetMapping("/isRegister")
    public AjaxResult isRegisterByPhone(@RequestParam String account){
        //判断当前用户是否存在
        SysUser sysUser = new SysUser();
        sysUser.setUserName(account);
        boolean result = sysUserService.checkUserNameUnique(sysUser);
        if(result) {
            return  AjaxResult.success("当前用户不存在", "success");
        }
        return AjaxResult.success("当前用户已存在，请直接登录", "error");
    }

    @GetMapping("/isRegisterCompany")
    public AjaxResult isRegisterByCompany(@RequestParam String company){
        //判断当前企业是否存在
        Integer res = sysDeptService.checkDeptNameExist(company);
        if(res > 0) {
            return  AjaxResult.success("当前企业已存在，请联系企业管理员", "error");
        }
        return AjaxResult.success("当前企业不存在", "success");
    }
}
